﻿using Microsoft.Extensions.Logging;
using NPOI.SS.Formula.Functions;
using ST.Common.Util;
using ST.Common.Util.Text;
using ST.Common.Util.Xml;
using ST.WX.Common.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Web;
using System.Xml;

namespace ST.WX.Common.Pay
{
	public class WXPayRequestPaymentData
	{
		/// <summary>
		/// 小程序ID
		/// </summary>
		public string appId { get; set; }

		/// <summary>
		/// 时间戳（时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间）
		/// </summary>
		public string timeStamp { get; set; }

		/// <summary>
		/// 随机字符串
		/// </summary>
		public string nonceStr { get; set; }

		/// <summary>
		/// 数据包
		/// </summary>
		public string package { get; set; }

		/// <summary>
		/// 签名方式
		/// </summary>
		public string signType { get; set; }

		/// <summary>
		/// 签名方式
		/// </summary>
		private WXPaySignType signMethod;

		/// <summary>
		/// 签名
		/// </summary>
		public string paySign { get; set; }

		private WXPayRequestPaymentData()
		{

		}

		public WXPayRequestPaymentData(string _appId, string _timeStamp, string _nonceStr, string prepay_id, string mchKey, WXPaySignType _signType)
		{
			appId = _appId;
			timeStamp = _timeStamp;
			nonceStr = _nonceStr;
			package = "prepay_id=" + prepay_id;
			signMethod = _signType;
			switch (_signType)
			{
				case WXPaySignType.MD5:
					signType = WXPayBase.SIGN_TYPE_MD5;
					break;
				case WXPaySignType.HMACSHA256:
					signType = WXPayBase.SIGN_TYPE_HMACSHA256;
					break;
			}

			//计算签名
			var dic = new SortedDictionary<string, object>();
			dic["appId"] = appId;
			dic["timeStamp"] = timeStamp;
			dic["nonceStr"] = nonceStr;
			dic["package"] = package;
			dic["signType"] = signType;
			paySign = WXPayBase.MakeSign(dic, mchKey, signMethod);
		}
	}
}
